import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { EventManager } from "../manager/EventManager";
import { UIManager } from "../manager/UIManager";
import GameUI from "./GameUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ReviveUI extends cc.Component {

    @property({ type: [cc.Node], tooltip: "节点" })
    private nodes: cc.Node[] = [];

    private isClick: boolean = false;
    private type: number = -1;

    protected async onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
    }

    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
    }

    private openUI(data) {
        // console.log("data:", data);
        this.type = data?.type;
        this.nodes.forEach((o, i) => {
            o.active = this.type == i;
        });
    }

    private onBtnsClicked(event: cc.Event, data: cc.Event.EventCustom) {
        const name = event.target.name;
        switch (name) {
            case "btnRevive":     // 复活
                {
                    if (this.isClick) { return; }
                    this.isClick = true;
                    AdManager.showVideoAd(() => {
                        this.isClick = false;
                        UIManager.ins.close();
                        GameUI.instance.revive(this.type);
                    }, () => {
                        this.isClick = false;
                    });
                }
                break;
            case "btnClose":     // 关闭
                {
                    UIManager.ins.close();
                    UIManager.ins.showPop("LoseUI", BUNDLE_TYPE_ENUM.GAME_PLAY);
                }
                break;
        }
        // console.log("onReviveClicked()===>按钮:", name);
    }
}